package com.cn.demo4.servlet.admin.company;

import com.alibaba.fastjson.JSONObject;
import com.cn.demo4.model.CompanyModel;
import com.cn.demo4.model.PageModel;
import com.cn.demo4.service.ICompanyService;
import com.cn.demo4.service.impl.CompanyServiceImpl;
import com.cn.demo4.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/company/findAll")
public class CompanyFindAllServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.乱码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");

        String name = req.getParameter("name");
        String address = req.getParameter("address");
        String phone = req.getParameter("phone");
        String status = req.getParameter("status");
        status = status == null ? "-1" : status;
        String curPage = req.getParameter("curPage");
        String pageSize = req.getParameter("pageSize");


        CompanyModel companyModel = new CompanyModel();
        companyModel.setName(name);
        companyModel.setPhone(phone);
        companyModel.setAddress(address);
        companyModel.setStatus(Tool.stringToInt(status));
        PageModel pageModel = new PageModel();
        pageModel.setCurPage(Tool.toCurPage(curPage));
        pageModel.setPageSize(Tool.toPageSize(pageSize));
        companyModel.setPageModel(pageModel);

        //调用服务逻辑层
        ICompanyService iCompanyService = new CompanyServiceImpl();

        //得到结果
        CompanyModel model = iCompanyService.findAll(companyModel);

        int count = iCompanyService.getCount(companyModel);

        //返回结果
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("model",model);
        jsonObject.put("count",count);
        resp.getWriter().println(jsonObject);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }

}
